package org.ww.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.ww.commonutils.commentvo.MemberComment;
import org.ww.commonutils.ordervo.CourseWebVoOrder;
import org.ww.order.bean.Order;
import org.ww.order.client.EduClient;
import org.ww.order.client.UcenterClient;
import org.ww.order.mapper.OrderMapper;
import org.ww.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ww.order.utils.HttpClient;
import org.ww.order.utils.OrderNoUtil;
import org.ww.servicebase.exceptionhandler.GuliException;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-11-11
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 添加订单
     * @param memberId
     * @param courseId
     * @return
     */
    @Override
    public String addOrder(String memberId, String courseId) {
        //1 调用服务：根据课程id获取课程信息
        CourseWebVoOrder courseInfo = eduClient.getCourseInfoOrder(courseId);
        //2 调用服务：根据用户id获取获取信息
        MemberComment memberInfo = ucenterClient.getUserInfoById(memberId);

        //创建订单对象
        Order order=new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());

        order.setCourseId(courseId);
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseInfo.getPrice());
        order.setMemberId(memberId);
        order.setMobile(memberInfo.getMobile());
        order.setNickname(memberInfo.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);
        return order.getOrderNo();
    }

    /**
     * 根据课程id和用户id查看该课程是否被购买
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public boolean isBuyCourse(String courseId, String memberId) {
        //封装条件，查询该课程是否被购买
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        return baseMapper.selectCount(wrapper)>0;
    }


}
